package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wangshuai
 * @since 2021-02-04
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    //课程大纲列表
    //根据课程id进行查询
    @GetMapping("chapterlist/{courseId}")
    public R getChapterList(@PathVariable String courseId) {

        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);

        return R.ok().data("list",list);
    }

    //添加章节
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {

        chapterService.save(eduChapter);

        return R.ok();
    }

    //根据id查询章节
    @GetMapping("getChapterById/{id}")
    public R getChapterById(@PathVariable String id) {

        EduChapter eduChapter = chapterService.getById(id);

        return R.ok().data("data",eduChapter);
    }

    //修改章节
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter) {

        chapterService.updateById(eduChapter);

        return R.ok();
    }

    //删除章节
    @DeleteMapping("deleteChapter/{id}")
    public R deleteChapter(@PathVariable String id) {

        Boolean flag = chapterService.deleteChapter(id);

        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

}

